package cn.xiangxu.telecom.login.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.stereotype.Repository;

import cn.xiangxu.telecom.login.entity.Admin;

@Repository("adminDao")
public class AdminDaoJdbcImpl implements Serializable, AdminDao {
	@Resource(name="dbcp")
	private BasicDataSource bds;
	
	public Admin findByAdminCode(String adminCode) {
		Connection conn = null;
		try {
			conn = bds.getConnection();
			String sql = "SELECT * "
					+ "FROM admin_info "
					+ "WHERE admin_code = ? ";
			System.out.println(sql);
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, adminCode);
			
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				Admin admin = new Admin();
				admin.setAdminId(rs.getInt("admin_id"));
				admin.setAdminCode("admin_code");
				admin.setPassword(rs.getString("password"));
				admin.setName(rs.getString("name"));
				admin.setTelephone(rs.getString("telephone"));
				admin.setEmail(rs.getString("email"));
				admin.setEnrolldate(rs.getTimestamp("enrolldate"));
				return admin;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if(null != conn) {
				try {
					conn.close();
				} catch(SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
		return null;
	}

}



















